import { Meeting, MeetingEntity } from '@/api/manager/meeting'
import { Mt, MtEntity } from '@/api/manager/mt'
import { IMcc } from '@/api/manager/type'
import { transEntityMtToMeeting } from '../../../../utils/translator'
import { getWeightExceptVip } from './utils'

export type mccCascadeVOs = (MeetingEntity & { mts: MtEntity[] })[]
export type ViewMccModel = Omit<MccModel<IMcc>, 'attr' | 'children'>
export interface MccModel<T> {
	mcc: T
	status: {
		isOpen: boolean
		isChecked: boolean
		isMatchSearch: boolean
		isCalling: boolean
	}
	attr: {
		weightExceptVipInSortType2: number
	}
	nestingLevel: number
	mtKey: string
	children: MccModel<IMcc>[]
}
export const transMccModel = <T extends IMcc>(mcc: T): MccModel<T> => {
	return {
		mcc,
		status: {
			isOpen: false,
			isChecked: false,
			isMatchSearch: true,
			isCalling: false,
		},
		attr: {
			weightExceptVipInSortType2: getWeightExceptVip(mcc),
		},
		nestingLevel: 0,
		mtKey: mcc.mtKey,
		children: [],
	}
}
export const transMtEntityList = (list: MtEntity[], parentId: string): [MccModel<Meeting>[], MccModel<Mt>[]] => {
	let meetingList = [],
		mtList = []
	for (const mcc of list) {
		//终端节点会存在离线的 meeting
		if (mcc.mt_type === 7) {
			let meeting = transEntityMtToMeeting(mcc, parentId)
			meetingList.push(transMccModel(new Meeting(meeting)))
		} else {
			mtList.push(transMccModel(new Mt(mcc)))
		}
	}
	return [meetingList, mtList]
}
export const transCascade = (mccCascadeVOs: mccCascadeVOs): [MccModel<Meeting>[], MccModel<Mt>[]] => {
	let meetingList = [] as MccModel<Meeting>[],
		mtList = [] as MccModel<Mt>[]
	for (const cascade of mccCascadeVOs) {
		//本级节点
		if (cascade.mt_key === '0.0.0.0_0') {
			const [meetings, mts] = transMtEntityList(cascade.mts, cascade.id)
			meetingList = [...meetingList, ...meetings]
			mtList = [...mtList, ...mts]
		}
		meetingList.push(transMccModel(new Meeting(cascade)))
	}
	return [meetingList, mtList]
}
